/**
 * 业务类
 */

package com.xczl.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.xczl.demo.dao.GoodsDao;
import com.xczl.demo.domain.Goods;

@Service
@Transactional
public class GoodsService {
	
	@Autowired
	private GoodsDao goodsDao;
	
	//添加
	public void add(Goods goods){
		Assert.notNull(goods);//实体是否存在
		Assert.hasText(goods.getName());//Dao插入数
		Assert.notNull(goods.getPrice());
		Assert.notNull(goods.getState());
		Assert.notNull(goods.getCateId());
		goodsDao.insertGoods(goods);
	}
	
	//修改
	public void update(Goods goods){
		Assert.notNull(goods,"商品实体不存在");//实体是否存在
		Assert.hasText(goods.getName(),"请填写商品名称");
		Assert.notNull(goods.getPrice(),"请填写商品价格");
		Assert.notNull(goods.getState(),"请填写商品状态");
		Assert.notNull(goods.getCateId(),"请填写商品分类");
		Assert.notNull(goods.getGoodsId(),"商品ID不存在");	
		//查询是否上架商品
		Goods temp=goodsDao.findById(goods.getGoodsId());
		Assert.notNull(temp,"商品实体不存在");//实体是否存在
		Assert.isTrue(temp.getState().equals(Goods.STATE_OFF),"请将商品下架后再修改");
		goodsDao.updateGoods(goods);
	}
	
	//删除
	public void delete(Goods goods){
		Assert.notNull(goods);//实体是否存在
		Assert.notNull(goods.getGoodsId());	
		goodsDao.deleteGoods(goods);
	}
	
	//查询
	public Goods findById(int id){
		return goodsDao.findById(id);
	}

	public void setState(int id, int state) {
		goodsDao.setState(id,state);
	}
}
